{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6b4ce4b8-9043-4d4e-9013-fadb8695d852",
   "metadata": {},
   "outputs": [],
   "source": [
    "%matplotlib inline\n",
    "%config InlineBackend.figure_format = 'retina'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8f16bac6-fe71-480f-a040-1d4c0af7c198",
   "metadata": {
    "panel-layout": {
     "height": 728.1375122070312,
     "visible": true,
     "width": 100
    },
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "x = np.arange(-10, 11)\n",
    "y1 = (x**2) / 5\n",
    "y2 = (x+50) / 5\n",
    "\n",
    "plt.plot(x, y1)\n",
    "plt.plot(x, y2)\n",
    "\n",
    "plt.axis('equal')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f6c8ccf7-b048-4888-a7b5-57719d82314d",
   "metadata": {},
   "outputs": [],
   "source": [
    "from matplotlib.font_manager import FontProperties\n",
    "\n",
    "font = FontProperties(fname='C:/Windows/Fonts/msyh.ttc', size=14)\n",
    "t = np.arange(1, 10, 0.05)\n",
    "x = np.sin(t)\n",
    "y = np.cos(t)\n",
    "\n",
    "plt.figure(figsize=(8, 5))  # 图像窗口大小\n",
    "plt.plot(x, y, 'b')  # 第三个参数表示图线颜色性状\n",
    "plt.axis('equal') # 等距刻度\n",
    "plt.xlabel('正弦', fontproperties=font)  # 轴标签, 图表名称 设置字体\n",
    "plt.ylabel('余弦', fontproperties=font)\n",
    "plt.title('圆形', fontproperties=font)\n",
    "\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "087928ca-01fe-47f4-bdba-47706bb22713",
   "metadata": {},
   "outputs": [],
   "source": [
    "import matplotlib\n",
    "\n",
    "matplotlib.rcParams['font.family'] = 'STSong'  # 全局配置图像字体\n",
    "matplotlib.rcParams['font.size'] = 12\n",
    "\n",
    "t = np.arange(1, 10, 0.05)\n",
    "x = np.sin(t)\n",
    "y = np.cos(t)\n",
    "\n",
    "plt.figure(figsize=(8, 5))  # 图像窗口大小\n",
    "plt.plot(x, y, 'r')  # 第三个参数表示图线颜色性状\n",
    "plt.axis('equal') # 等距刻度\n",
    "plt.xlabel('正弦')\n",
    "plt.ylabel('余弦')\n",
    "plt.title('圆形')\n",
    "\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f4adabd2-444d-40f8-858c-35c8ec701da2",
   "metadata": {},
   "outputs": [],
   "source": [
    "x1 = np.linspace(0.0, 5.0)\n",
    "x2 = np.linspace(0.0, 2.0)\n",
    "\n",
    "y1 = np.cos(2 * np.pi * x1) * np.exp(-x1)\n",
    "y2 = np.cos(2 * np.pi * x2)\n",
    "\n",
    "plt.subplot(2,1,1) # 指定二维表中图表的位置, 前两个数字为形状, 第三个数字表示表中的索引位置; 当3个数字都小于10时可以用一个整数211表示\n",
    "plt.plot(x1, y1, 'bo-')  # y黄色 r红色, b蓝, g绿, o实心大圆点,  .实心小圆点, -连线\n",
    "plt.title('y = f(n)')\n",
    "\n",
    "plt.subplot(2,1,2)\n",
    "plt.plot(x2, y2, 'g.-')\n",
    "plt.xlabel('times')\n",
    "plt.ylabel('Undap')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "49aaf7e1-625b-401b-95b0-2dfad346fcf6",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "from scipy.stats import norm\n",
    "\n",
    "mu = 100\n",
    "sigma = 15\n",
    "x = mu + sigma * np.random.randn(10000)\n",
    "print('x: ', x.shape)\n",
    "\n",
    "num_bins = 50\n",
    "n, bins, patches = plt.hist(x, bins=num_bins, density=0, facecolor='g', alpha=0.5)  # bins直方图柱数默认10, density频率分布\n",
    "y = norm.pdf(bins, mu, sigma)\n",
    "plt.plot(bins, y, 'r--')\n",
    "plt.xlabel('Smarts')\n",
    "plt.ylabel('Probability')\n",
    "plt.title('Histogram of IO: $\\Mu=100$, $\\Sigma=15$')  # $$强调并支持转义\n",
    "plt.subplots_adjust(left=0.15)\n",
    "plt.show()\n",
    "print(\"bind:\\n\", bins)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "db067cfd-34b9-44b9-af44-adbbff32bcdf",
   "metadata": {},
   "outputs": [],
   "source": [
    "from matplotlib import cm\n",
    "from mpl_toolkits.mplot3d import Axes3D\n",
    "\n",
    "delta = 0.2\n",
    "x = np.arange(-3, 3, delta)\n",
    "y = np.arange(-3, 3, delta)\n",
    "\n",
    "X,Y = np.meshgrid(x, y)  # 简单的复制x,y形成两个矩阵X,Y; 两矩阵可组成矩形(xn,yn)点阵; https://www.cnblogs.com/lemonbit/p/7593898.html\n",
    "Z = X**2 + Y**2\n",
    "x = X.flatten()  # 展开np, array, mat对象为数组\n",
    "y = Y.flatten()\n",
    "z = Z.flatten()\n",
    "\n",
    "# https://matplotlib.org/stable/api/_as_gen/matplotlib.figure.Figure.html#\n",
    "fig = plt.figure(figsize=(12, 6)) # figsize 图片宽高, 默认6.4 * 4.8 inche\n",
    "\n",
    "ax1 = fig.add_subplot(121, projection='3d')\n",
    "# https://matplotlib.org/stable/api/_as_gen/mpl_toolkits.mplot3d.axes3d.Axes3D.plot_trisurf.html#\n",
    "ax1.plot_trisurf(x, y, z, cmap=cm.jet, linewidth=0.01)  # 绘制三维坐标系曲面图, 3D图像\n",
    "plt.title('3D')\n",
    "\n",
    "ax2 = fig.add_subplot(122)\n",
    "cs = ax2.contour(X, Y, Z, 15, cmap='jet')   # 绘制等高线图\n",
    "ax2.clabel(cs, inline=True, fontsize=10, fmt='%1.1f')  # ContoursSet 上设置图线的标签\n",
    "plt.title('Contour')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "929ea83f-318e-459f-8a46-795e2f7a693d",
   "metadata": {},
   "outputs": [],
   "source": [
    "fig = plt.figure(figsize=(8,6))\n",
    "ax = fig.add_subplot(projection='3d')\n",
    "ax.plot_surface(X,Y,Z,  rstride=1, cstride=1, alpha=0.5)  # stride采样步幅, r -> Y, c -> X, 步幅越小, 采样越多, 默认1, alpha透明度\n",
    "\n",
    "# 绘制3d图像的投影 (切面)\n",
    "cset = ax.contour(X,Y,Z,zdir='z', offset=0, cmap=cm.coolwarm)\n",
    "cset = ax.contour(X,Y,Z,zdir='x', offset=-3, cmap=cm.coolwarm)\n",
    "cset = ax.contour(X,Y,Z,zdir='y', offset=3, cmap=cm.coolwarm)\n",
    "ax.set_xlabel('X')\n",
    "ax.set_xlim(-3, 3)\n",
    "ax.set_ylabel('Y')\n",
    "ax.set_ylim(-3, 3)\n",
    "ax.set_zlabel('Z')\n",
    "ax.set_zlim(0, 16)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0ce207fd-2cf5-4951-ab4f-526d80c6231f",
   "metadata": {},
   "outputs": [],
   "source": [
    "gp = 5\n",
    "means_men = (20,35,30,35,27)\n",
    "std_men = (2,1,4,1,2)\n",
    "\n",
    "means_wm = (25,32,34,20,25)\n",
    "std_wm = (3,5,2,3,3)\n",
    "\n",
    "fig,ax = plt.subplots()\n",
    "index = np.arange(gp)\n",
    "bar_width = 0.35\n",
    "\n",
    "opacity = 0.4\n",
    "error_conf = {'ecolor': '0.3'}\n",
    "\n",
    "# yerr 基于标准值的误差\n",
    "rects1 = plt.bar(x=index, height=means_men, width=bar_width, alpha=opacity, color='b', yerr=std_men, error_kw=error_conf, label='Men')\n",
    "rects1 = plt.bar(x=index+bar_width, height=means_wm, width=bar_width, alpha=opacity, color='r', yerr=std_wm, error_kw=error_conf, label='Wm')\n",
    "\n",
    "plt.xlabel('Group')\n",
    "plt.ylabel('Scores')\n",
    "plt.title('Scores by group and gender')\n",
    "plt.xticks(index+bar_width, ('A', 'B', 'C', 'D', 'E'))\n",
    "plt.legend()  # 图例\n",
    "plt.tight_layout()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "664944e3-cedb-4287-97cc-8614420190d3",
   "metadata": {},
   "outputs": [],
   "source": [
    "labels = ['Frogs', 'Hogs', 'Dogs', 'Logs']\n",
    "size = [15, 30, 45, 10]\n",
    "colors = ['yellowgreen', 'gold', 'lightskyblue', 'lightcoral'] \n",
    "explode = (0, 0.1, 0, 0)\n",
    "plt.pie(x=size, explode=explode, labels=labels, colors=colors, autopct='%1.1f%%', shadow=True, startangle=90)\n",
    "plt.axis('equal')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "86b037df-1f2e-4134-9bb1-95335b754a44",
   "metadata": {},
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
    "\n",
    "import matplotlib.cbook as cbook\n",
    "\n",
    "np.set_printoptions(suppress=True)\n",
    "\n",
    "# Load a numpy record array from yahoo csv data with fields date, open, high,\n",
    "# low, close, volume, adj_close from the mpl-data/sample_data directory. The\n",
    "# record array stores the date as an np.datetime64 with a day unit ('D') in\n",
    "# the date column.\n",
    "price_data = cbook.get_sample_data('goog.npz')['price_data']\n",
    "price_data = price_data[-250:]  # get the most recent 250 trading days\n",
    "\n",
    "print(price_data.dtype)\n",
    "\n",
    "# ndarrays\n",
    "#  [('2007-10-18', 635.41, 641.37, 628.5 , 639.62, 12289200, 639.62)\n",
    "#   ('2007-10-19', 654.56, 658.49, 643.23, 644.71, 15789000, 644.71)...]\n",
    "delta1 = np.diff(price_data[\"adj_close\"]) / price_data[\"adj_close\"][:-1]\n",
    "# diff 计算离散差(Xi+1 - Xi), 结果的个数比样本数少一个, 计算增量百分比就不除以最后一个样本\n",
    "\n",
    "\n",
    "# Marker size in units of points^2\n",
    "volume = (15 * price_data[\"volume\"][:-2] / price_data[\"volume\"][0])**2\n",
    "close = 0.003 * price_data[\"close\"][:-2] / 0.003 * price_data[\"open\"][:-2]\n",
    "\n",
    "fig, ax = plt.subplots()\n",
    "ax.scatter(delta1[:-1], delta1[1:], c=close, s=volume, alpha=0.5)\n",
    "\n",
    "ax.set_xlabel(r'$\\Delta_i$', fontsize=15)\n",
    "ax.set_ylabel(r'$\\Delta_{i+1}$', fontsize=15)\n",
    "ax.set_title('Volume and percent change')\n",
    "\n",
    "ax.grid(True)\n",
    "fig.tight_layout()\n",
    "\n",
    "plt.show()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.7"
  },
  "panel-cell-order": [
   "8f16bac6-fe71-480f-a040-1d4c0af7c198"
  ]
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
